package offer;

import java.util.Comparator;
import java.util.PriorityQueue;

public class offerII_0076 {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(new Comparator<>(){

            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        int num=0;
        int len=nums.length;
        for(int i=0;i<len;++i){
            if(num>=k&&!priorityQueue.isEmpty()&&priorityQueue.peek()>=nums[i]) continue;
            priorityQueue.add(nums[i]);
            num++;
        }
        for(int i=0;i<num-k;++i){
            priorityQueue.poll();
        }
        return priorityQueue.peek();
    }
}
